package com.linsen.erp.admin.controller;

import com.linsen.erp.admin.provider.dto.UserDTO;
import com.linsen.erp.admin.provider.qo.UserListQo;
import com.linsen.erp.admin.provider.qo.UserQo;
import com.linsen.erp.admin.provider.vo.UserVO;
import com.linsen.erp.admin.service.IUserService;
import com.linsen.core.common.util.Result;
import com.linsen.core.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhaoyong
 * @since 2023-07-24
 */
@RestController
@RequestMapping("/admin/user")
@Api(tags = "用户管理")
@RequiredArgsConstructor
public class UserController {

    private final IUserService iUserService;

    @GetMapping("current")
    @ApiOperation(value = "当前登录用户信息",response = UserVO.class)
    public Result current() {
        return Result.data(this.iUserService.current());
    }

    @Log(value = "修改当前登录用户信息")
    @PostMapping("updateCurrent")
    @ApiOperation("修改当前登录用户信息")
    public Result updateCurrent(@RequestBody UserDTO userDTO) {
        iUserService.updateCurrent(userDTO);

        return Result.ok();
    }

    //@SaCheckPermission("admin:user:page")
    @PostMapping("/page")
    @ApiOperation(value = "分页查询",response = UserVO.class)
    public Result page(@RequestBody UserQo userQo) {
        return Result.data(this.iUserService.page(userQo));
    }

    @PostMapping("/list")
    @ApiOperation(value = "用户下拉列表查询",response = UserVO[].class)
    public Result list(@RequestBody UserListQo userListQo) {
        return Result.data(this.iUserService.list(userListQo));
    }

    @GetMapping("info")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "string")
    @ApiOperation(value = "用户详情",response = UserVO.class)
    public Result get(@ApiIgnore @RequestParam(value = "id") String id) {
        return Result.data(this.iUserService.get(id));
    }

    //@SaCheckPermission("admin:user:save")
    @Log(value = "新增用户")
    @PostMapping("save")
    @ApiOperation("新增用户")
    public Result save(@RequestBody UserDTO userDTO) {
        iUserService.saveUser(userDTO);

        return Result.ok();
    }

    //@SaCheckPermission("admin:user:update")
    @Log(value = "修改用户")
    @PostMapping("update")
    @ApiOperation("修改用户")
    public Result update(@RequestBody UserDTO userDTO) {
        iUserService.updateUser(userDTO);

        return Result.ok();
    }

    //@SaCheckPermission("admin:user:useStatus")
    @Log(value = "切换冻结状态")
    @PostMapping("useStatus")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "string")
    @ApiOperation("切换冻结状态")
    public Result useStatus(@ApiIgnore @RequestParam("id") String id) {
        iUserService.useStatus(id);

        return Result.ok();
    }

    //@SaCheckPermission("admin:user:delete")
    @Log(value = "删除用户")
    @PostMapping("delete")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "string")
    @ApiOperation("删除用户")
    public Result delete(@ApiIgnore @RequestParam("id") String id) {
        iUserService.delete(id);

        return Result.ok();
    }

    //@SaCheckPermission("admin:user:resetPassword")
    @Log(value = "重置密码")
    @PostMapping("resetPassword")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "string")
    @ApiOperation("重置密码")
    public Result resetPassword(@ApiIgnore @RequestParam("id") String id) {
        iUserService.resetPassword(id);

        return Result.ok();
    }

}
